package yxy.cherry.battle.module.selector;

import yxy.cherry.battle.Fighter;
import yxy.cherry.battle.Team;
import yxy.cherry.battle.module.Position;

import java.util.HashSet;
import java.util.Set;

/**
 * 本方自己这列优先选队友,队友死了选自己
 *
 * @ClassName TargetSelector_38_自己这列优先队友
 * @Author jingtao-zheng
 * @Date 2022/9/16 22:35
 * @Version 1.0
 **/
@TargetSelectorType(Type = 38)
public class TargetSelector_38_自己这行优先队友 extends TargetSelector {
    @Override
    public Set<Fighter> target(Fighter actor, Team team, int param1, int param2) {
        Position position = actor.getPosition();
        Position targetPosition = genTargetPosition(position, team);
        switch (targetPosition) {
            case FU:
            case BU: {
                Set<Fighter> set1 = getRow(targetPosition, team);
                return chooseTarget(set1, param1, param2, actor);
            }
            case FM:
            case BM: {
                Set<Fighter> set2 = getRow(targetPosition, team);
                return chooseTarget(set2, param1, param2, actor);
            }
            case FD:
            case BD: {
                Set<Fighter> set3 = getRow(targetPosition, team);
                return chooseTarget(set3, param1, param2, actor);
            }
            default:
                return null;
        }
    }

    private Set<Fighter> chooseTarget(Set<Fighter> set, int param1, int param2, Fighter actor) {
        //本方自己这列优先选队友,队友死了选自己
        if (set == null || set.size() <= 0) {
            return null;
        }
        if (set.size()==1){
            return set;
        }
        for (Fighter fighter : set) {
            if (actor.equals(fighter)) {
                continue;
            }else{
                Set<Fighter> targetSet = new HashSet<>();
                targetSet.add(fighter);
                return targetSet;
            }
        }
        return null;
    }


    /**
     * 根据施法者的位置先选定基准目标位置
     *
     * @param position
     * @return
     */
    private Position genTargetPosition(Position position, Team team) {
        switch (position) {
            case FU:
            case BU: {
                Position positions[] = {Position.FU, Position.FM, Position.FD, Position.BU, Position.BM, Position.BD};
                return genPosition(positions, team);
            }
            case FM:
            case BM: {
                Position positions[] = {Position.FM, Position.FU, Position.FD, Position.BM, Position.BU, Position.BD};
                return genPosition(positions, team);
            }
            case FD:
            case BD: {
                Position positions[] = {Position.FD, Position.FU, Position.FM, Position.BD, Position.BU, Position.BM};
                return genPosition(positions, team);
            }
        }
        return null;
    }

    /**
     * 获取有英雄的位置
     *
     * @param positions
     * @param team
     * @return
     */
    private Position genPosition(Position[] positions, Team team) {
        for (Position position : positions) {
            if (team.getFighterByPosition(position) != null && team.getFighterByPosition(position).is活着()) {
                return position;
            }
        }
        return null;
    }
}
